home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / DO_NAME.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  14KB  |  635 lines

  1. /*    SCCS Id: @(#)do_name.c    3.0    89/11/08
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6.  
  7. #ifdef NAMED_ITEMS
  8. # include <ctype.h>
  9. #endif
  10.  
  11. #ifdef OVLB
  12.  
  13. static char *FDECL(visctrl, (CHAR_P));
  14. static void FDECL(do_oname, (struct obj *));
  15.  
  16. static
  17. char *
  18. visctrl(c)
  19. char c;
  20. {
  21. #ifdef LINT    /* static char ccc[3]; */
  22.     char ccc[3];
  23. #else
  24.     static char ccc[3];
  25. #endif
  26.  
  27.     if(c < 040) {
  28.         ccc[0] = '^';
  29.         ccc[1] = c + 0100;
  30.         ccc[2] = 0;
  31.     } else {
  32.         ccc[0] = c;
  33.         ccc[1] = 0;
  34.     }
  35.     return(ccc);
  36. }
  37.  
  38. void
  39. getpos(cc,force,goal)
  40. coord    *cc;
  41. int force;
  42. const char *goal;
  43. {
  44.     register int cx, cy, i, c;
  45.     const char *sdp = flags.num_pad ? ndir : sdir;
  46. #ifdef MACOS
  47.     extern short    macflags;
  48.     Boolean    fUpdateFlagOn;
  49.     long    ticks;
  50. #endif
  51.  
  52.     if(flags.verbose) pline("(For instructions type a ?)");
  53. #ifdef MACOS    
  54.     if ((macflags & fDoUpdate) && (macflags & fDoNonKeyEvt)) {
  55.         fUpdateFlagOn = true;
  56.     } else {
  57.         fUpdateFlagOn = false;
  58.         macflags |= (fDoUpdate | fDoNonKeyEvt);
  59.     }
  60.     macflags |= fMoveWRTMouse;
  61. #endif
  62.     cx = cc->x;
  63.     cy = cc->y;
  64. #ifdef CLIPPING
  65.     cliparound(cx, cy);
  66.     (void) win_curs(cx, cy);
  67. #else
  68.     curs(cx,cy+2);
  69. #endif
  70.     while((c = readchar()) != '.'){
  71.         for(i=0; i<8; i++) if(sdp[i] == c){
  72.             if(1 <= cx + xdir[i] && cx + xdir[i] <= COLNO)
  73.                 cx += xdir[i];
  74.             if(0 <= cy + ydir[i] && cy + ydir[i] <= ROWNO-1)
  75.                 cy += ydir[i];
  76.             goto nxtc;
  77.         }
  78.         if(c == '?'){
  79.             pline("Use [%s] to move the cursor to %s.",
  80.                   flags.num_pad ? "2468" : "hjkl", goal);
  81.             pline("Type a . when you are at the right place.");
  82.         } else {
  83.             if (!index(quitchars, c))
  84.                 pline("Unknown direction: '%s' (%s).",
  85.                 visctrl(c),
  86.                 force ?
  87.                     flags.num_pad ? "use 2468 or ." :
  88.                             "use hjkl or ." :
  89.                     "aborted");
  90.             if(force) goto nxtc;
  91. #ifdef MACOS
  92.             macflags &= ~fMoveWRTMouse;
  93.             if (!fUpdateFlagOn)
  94.                 macflags &= ~(fDoUpdate | fDoNonKeyEvt);
  95. #endif
  96.             cc->x = -1;
  97.             cc->y = 0;
  98.             return;
  99.         }
  100.     nxtc:    ;
  101. #ifdef CLIPPING
  102.         cliparound(cx, cy);
  103.         (void) win_curs(cx, cy);
  104. #else
  105.         curs(cx,cy+2);
  106. #endif
  107.     }
  108. #ifdef MACOS
  109.     macflags &= ~fMoveWRTMouse;
  110.     if (!fUpdateFlagOn)
  111.         macflags &= ~(fDoUpdate | fDoNonKeyEvt);
  112. #endif
  113.     cc->x = cx;
  114.     cc->y = cy;
  115.     return;
  116. }
  117.  
  118. struct monst *
  119. christen_monst(mtmp, name)
  120. struct monst *mtmp;
  121. const char *name;
  122. {
  123.     register int lth,i;
  124.     register struct monst *mtmp2;
  125.  
  126.     /* dogname and catname are 63-character arrays; the generic naming
  127.      * function do_mname() below also cut names off at 63 characters */
  128.     lth = strlen(name)+1;
  129.     if(lth > 63){
  130.         lth = 63;
  131.     }
  132.     mtmp2 = newmonst(mtmp->mxlth + lth);
  133.     *mtmp2 = *mtmp;
  134.     for(i=0; i<mtmp->mxlth; i++)
  135.         ((char *) mtmp2->mextra)[i] = ((char *) mtmp->mextra)[i];
  136.     mtmp2->mnamelth = lth;
  137.     (void)strncpy(NAME(mtmp2), name, lth);
  138.     NAME(mtmp2)[lth-1] = 0;
  139.     replmon(mtmp,mtmp2);
  140.     return(mtmp2);
  141. }
  142.  
  143. int
  144. do_mname(){
  145.     char buf[BUFSZ];
  146.     coord cc;
  147.     register int cx,cy;
  148.     register struct monst *mtmp;
  149.     register char *curr;
  150.     boolean blank;
  151.  
  152.     cc.x = u.ux;
  153.     cc.y = u.uy;
  154.     getpos(&cc, 0, "the monster you want to name");
  155.     cx = cc.x;
  156.     cy = cc.y;
  157.     if(cx < 0) return(0);
  158.     if (cx == u.ux && cy == u.uy) {
  159.         pline("This %s creature is called %s and cannot be renamed.",
  160.         ACURR(A_CHA) > 14 ?
  161.         (flags.female ? "beautiful" : "handsome") :
  162.         "ugly",
  163.         plname);
  164.         return(0);
  165.     }
  166.     if (!cansee(cx,cy) || !MON_AT(cx,cy) || (mtmp = m_at(cx, cy))->mimic
  167.             || (mtmp->minvis && !See_invisible) || mtmp->mundetected) {
  168.         pline("I see no monster there.");
  169.         return(0);
  170.     }
  171.     pline("What do you want to call %s? ", lmonnam(mtmp));
  172.     getlin(buf);
  173.     clrlin();
  174.     if(!*buf || *buf == '\033') return(0);
  175.  
  176.     /* unnames monster if all spaces */
  177.     for (curr = buf, blank = 1; *curr; blank = (*curr++ == ' '));
  178.     if(blank) *buf = '\0';
  179.  
  180.      if(type_is_pname(mtmp->data)) {
  181.          pline("%s doesn't like being called names!", Monnam(mtmp));
  182.          if(!mtmp->mtame) {
  183.          pline("%s gets %sangry!", Monnam(mtmp),
  184.                mtmp->mpeaceful ? "" : "very ");
  185.          mtmp->mpeaceful = 0;
  186.         mtmp->msleep = 0;
  187.          }
  188.          return(0);
  189.      }
  190.     (void) christen_monst(mtmp, buf);
  191.     return(0);
  192. }
  193.  
  194. /*
  195.  * This routine changes the address of obj. Be careful not to call it
  196.  * when there might be pointers around in unknown places. For now: only
  197.  * when obj is in the inventory.
  198.  */
  199. static
  200. void
  201. do_oname(obj)
  202. register struct obj *obj;
  203. {
  204.     char buf[BUFSZ];
  205.     register char *curr;
  206.     boolean blank;
  207.  
  208.     pline("What do you want to name %s? ", doname(obj));
  209.     getlin(buf);
  210.     clrlin();
  211.     if(!*buf || *buf == '\033')    return;
  212.  
  213.     /* unnames item if all spaces */
  214.     for (curr = buf, blank = 1; *curr; blank = (*curr++ == ' '));
  215.     if(blank) *buf = '\0';
  216.  
  217. #ifdef NAMED_ITEMS
  218.     if(is_artifact(obj))
  219.         pline("The artifact seems to resist the attempt.");
  220.     else if (restr_name(obj, buf) || exist_artifact(obj, buf)) {
  221.         int n = rn2(strlen(buf));
  222.         char c1,c2;
  223.  
  224.         c1 = isupper(buf[n]) ? tolower(buf[n]) : buf[n];
  225.         while (c1 == (c2 = 'a' + rn2('z'-'a')));
  226.         if (isupper(buf[n]))
  227.             /* islower(c2) guaranteed by generation */
  228.             buf[n] = toupper(c2);
  229.         else buf[n] = c2;
  230.         pline("While engraving your hand slips.");
  231.         more();
  232.         You("engrave: \"%s\".",buf);
  233.         (void)oname(obj, buf, 1);
  234.     }
  235.     else
  236. #endif
  237.         (void)oname(obj, buf, 1);
  238. }
  239.  
  240. struct obj *
  241. oname(obj, buf, ininv)
  242. register struct obj *obj;
  243. const char    *buf;
  244. register int ininv;
  245. {
  246.     register struct obj *otmp, *otmp2, *contents;
  247.     register int    lth;
  248.  
  249.     lth = *buf ? strlen(buf)+1 : 0;
  250.     if(lth > 63){
  251.         lth = 63;
  252.     }
  253.     /* if already properly named */
  254.     if(lth == obj->onamelth && (!lth || !strcmp(ONAME(obj),buf)))
  255.         return obj;
  256. #ifdef NAMED_ITEMS
  257.     /* If named artifact exists in the game, do not create another.
  258.      * Also trying to create an artifact shouldn't de-artifact
  259.      * it (e.g. Excalibur from prayer). In this case the object
  260.      * will retain its current name. */
  261.     if (is_artifact(obj) || exist_artifact(obj, buf))
  262.         return obj;
  263.     else
  264.         artifact_exists(obj, buf, TRUE);
  265. #endif
  266.     otmp2 = newobj(lth);
  267.     *otmp2 = *obj;
  268.     otmp2->onamelth = lth;
  269. #ifdef __GNUC__
  270.     /* Without the following line, the program gives anything an empty
  271.      * name when I try to #name it.  Probably a compiler bug, but at the
  272.      * point where I discovered this, there's no time to check to make
  273.      * sure.
  274.      */
  275.     if (buf) (void)donull();
  276. #endif
  277.     if(lth) {
  278.         (void)strncpy(ONAME(otmp2), buf, lth);
  279.         ONAME(otmp2)[lth-1] = 0;
  280.     }
  281.     if (obj->owornmask) {
  282.         /* Note: dying by burning in Hell causes problems if you
  283.          * try doing this when owornmask isn't set.
  284.          */
  285.         setworn((struct obj *)0, obj->owornmask);
  286.         setworn(otmp2, otmp2->owornmask);
  287.     }
  288.  
  289.     if (ininv) {
  290.         /* do freeinv(obj); etc. by hand in order to preserve
  291.            the position of this object in the inventory */
  292.         if(obj == invent) invent = otmp2;
  293.         else for(otmp = invent; ; otmp = otmp->nobj){
  294.             if(!otmp)
  295.                 panic("oname: cannot find obj.");
  296.             if(otmp->nobj == obj){
  297.                 otmp->nobj = otmp2;
  298.                 break;
  299.             }
  300.         }
  301.     }
  302.     if (Is_container(obj)) {
  303.         for(contents=fcobj; contents; contents=contents->nobj)
  304.             if(contents->cobj==obj) contents->cobj = otmp2;
  305.     }
  306.     /* obfree(obj, otmp2);    /* now unnecessary: no pointers on bill */
  307.     free((genericptr_t) obj);    /* let us hope nobody else saved a pointer */
  308.     return otmp2;
  309. }
  310.  
  311. static const char NEARDATA callable[] = {
  312.     SCROLL_SYM, POTION_SYM, WAND_SYM, RING_SYM, AMULET_SYM, GEM_SYM,
  313. #ifdef SPELLS
  314.     SPBOOK_SYM,
  315. #endif
  316.     ARMOR_SYM, TOOL_SYM, 0 };
  317.  
  318. int
  319. ddocall()
  320. {
  321.     register struct obj *obj;
  322. #ifdef REDO
  323.     char    ch;
  324.  
  325.     if (!in_doagain)
  326. #endif
  327.         pline("Name an individual object? ");
  328.     switch(
  329. #ifdef REDO
  330.         ch = 
  331. #endif
  332.         ynq()) {
  333.     case 'q':
  334.         break;
  335.     case 'y':
  336. #ifdef REDO
  337.         savech(ch);
  338. #endif
  339.         obj = getobj("#", "name");
  340.         if(obj) do_oname(obj);
  341.         break;
  342.     default:
  343. #ifdef REDO
  344.         savech(ch);
  345. #endif
  346.         obj = getobj(callable, "call");
  347.         if(obj) docall(obj);
  348.     }
  349.     return 0;
  350. }
  351.  
  352. void
  353. docall(obj)
  354. register struct obj *obj;
  355. {
  356.     char buf[BUFSZ];
  357.     struct obj otemp;
  358.     register char **str1;
  359.     register char *str;
  360.     boolean blank;
  361.  
  362.     otemp = *obj;
  363.     otemp.quan = 1;
  364.     otemp.onamelth = 0;
  365.     if (otemp.corpsenm) { /* kludge, meaning it's sink water */
  366.         pline("Call a stream of %s fluid: ",
  367.                 objects[otemp.otyp].oc_descr);
  368.     } else
  369.         pline("Call %s: ", an(xname(&otemp)));
  370.     getlin(buf);
  371.     clrlin();
  372.     if(!*buf || *buf == '\033')
  373.         return;
  374.  
  375.     /* clear old name */
  376.     str1 = &(objects[obj->otyp].oc_uname);
  377.     if(*str1) free((genericptr_t)*str1);
  378.  
  379.     /* uncalls item if all spaces */
  380.     for (str = buf, blank = 1; *str; blank = (*str++ == ' '));
  381.     if(blank) *buf = '\0';
  382.     if (!*buf) {
  383.         *str1 = NULL;
  384.         return;
  385.     }
  386.  
  387.     str = (char *) alloc((unsigned)strlen(buf)+1);
  388.     Strcpy(str,buf);
  389.     *str1 = str;
  390. }
  391.  
  392. #endif /*OVLB*/
  393. #ifdef OVL0
  394.  
  395. static const char *ghostnames[] = {
  396.     /* these names should have length < PL_NSIZ */
  397.     /* Capitalize the names for aesthetics -dgk */
  398.     "Adri", "Andries", "Andreas", "Bert", "David", "Dirk", "Emile",
  399.     "Frans", "Fred", "Greg", "Hether", "Jay", "John", "Jon", "Karnov",
  400.     "Kay", "Kenny", "Kevin", "Maud", "Michiel", "Mike", "Peter", "Robert",
  401.     "Ron", "Tom", "Wilmar", "Nick Danger", "Phoenix", "Havok",
  402.     "Stephan", "Lance Braccus", "Shadowhawk"
  403. };
  404.  
  405. char *
  406. x_monnam(mtmp, vb)
  407. register struct monst *mtmp;
  408. int vb;
  409. {
  410. #ifdef LINT    /* static char buf[BUFSZ]; */
  411.     char buf[BUFSZ];
  412. #else
  413.     static char buf[BUFSZ];
  414. #endif
  415.     boolean isinvis = (mtmp->minvis && mtmp->data != &mons[PM_STALKER]
  416.                 && mtmp->data != &mons[PM_GHOST]);
  417.  
  418.     buf[0] = '\0';
  419. #if defined(ALTARS) && defined(THEOLOGY)
  420.     if(mtmp->ispriest) return(priestname(mtmp));
  421. #endif
  422.     if(mtmp->isshk) {
  423.         Strcpy(buf, shkname(mtmp));
  424.         if (mtmp->data == &mons[PM_SHOPKEEPER] && !mtmp->minvis)
  425.             return(buf);
  426.         /* For normal shopkeepers, just 'Asidonhopo'.
  427.          * For unusual ones, 'Asidonhopo the invisible shopkeeper'
  428.          * or 'Asidonhopo the blue dragon'.
  429.          */
  430.         Strcat(buf, " ");
  431.     } else if(mtmp->mnamelth && !vb) {
  432.         if(isinvis) {
  433.             Strcpy(buf, "the invisible ");
  434.             Strcat(buf, NAME(mtmp));
  435.         } else 
  436.             Strcpy(buf, NAME(mtmp));
  437.         return(buf);
  438.     }
  439.  
  440.     switch(mtmp->data->mlet) {
  441.         case S_GHOST:
  442.         { register const char *gn = (const char *) mtmp->mextra;
  443.           if(!*gn) {        /* might also look in scorefile */
  444.             gn = ghostnames[rn2(SIZE(ghostnames))];
  445.             Strcpy((char *) mtmp->mextra, !rn2(5) ? (const char *)plname : gn);
  446.           }
  447.           if (Hallucination) {
  448.             Strcat(buf, "the ");
  449.             Strcat(buf, rndmonnam());
  450.           }
  451.           else
  452.             Sprintf(buf, "%s's ghost", (char *) mtmp->mextra);
  453.         }
  454.         break;
  455.         default:
  456.         if (mtmp->minvis)
  457.             Strcat(buf, "the invisible ");
  458.         else if (!type_is_pname(mtmp->data) || Hallucination
  459.                 || mtmp->data == &mons[PM_WIZARD_OF_YENDOR])
  460.             Strcat(buf, "the ");
  461.         Strcat(buf, Hallucination ? rndmonnam() : mtmp->data->mname);
  462.     }
  463.     if(vb && mtmp->mnamelth) {
  464.         Strcat(buf, " called ");
  465.         Strcat(buf, NAME(mtmp));
  466.     }
  467.     return(buf);
  468. }
  469.  
  470. #endif /* OVL0 */
  471. #ifdef OVLB
  472.  
  473. char *
  474. lmonnam(mtmp)
  475. register struct monst *mtmp;
  476. {
  477.     return(x_monnam(mtmp, 1));
  478. }
  479.  
  480. #endif /* OVLB */
  481. #ifdef OVL0
  482.  
  483. char *
  484. mon_nam(mtmp)
  485. register struct monst *mtmp;
  486. {
  487.     return(x_monnam(mtmp, 0));
  488. }
  489.  
  490. char *
  491. Monnam(mtmp)
  492. register struct monst *mtmp;
  493. {
  494.     register char *bp = mon_nam(mtmp);
  495.  
  496.     if('a' <= *bp && *bp <= 'z') *bp += ('A' - 'a');
  497.     return(bp);
  498. }
  499.  
  500. #endif /* OVL0 */
  501. #ifdef OVLB
  502.  
  503. char *
  504. a_monnam(mtmp,adj)
  505. register struct monst *mtmp;
  506. register const char *adj;
  507. {
  508.     register char *bp = mon_nam(mtmp);
  509. #ifdef LINT    /* static char buf[BUFSZ]; */
  510.     char buf[BUFSZ];
  511. #else
  512.     static char buf[BUFSZ];
  513. #endif
  514.  
  515.     if(!strncmp(bp, "the ", 4)) bp += 4;
  516.     Sprintf(buf, "the %s %s", adj, bp);
  517.     return(buf);
  518. }
  519.  
  520. /* sometimes we don't want an article in front of definite names */
  521.  
  522. char *
  523. a2_monnam(mtmp,adj)
  524. register struct monst *mtmp;
  525. register const char *adj;
  526. {
  527.     register char *bp = mon_nam(mtmp);
  528. #ifdef LINT    /* static char buf[BUFSZ]; */
  529.     char buf[BUFSZ];
  530. #else
  531.     static char buf[BUFSZ];
  532. #endif
  533.  
  534.     if(!strncmp(bp, "the ", 4))
  535.         Sprintf(buf, "the %s %s", adj, bp+4);
  536.     else
  537.         Sprintf(buf, "%s %s", adj, bp);
  538.     return(buf);
  539. }
  540.  
  541. char *
  542. Amonnam(mtmp, adj)
  543. register struct monst *mtmp;
  544. register const char *adj;
  545. {
  546.     register char *bp = a_monnam(mtmp,adj);
  547.  
  548.     *bp = 'T';
  549.     return(bp);
  550. }
  551.  
  552. char *
  553. Xmonnam(mtmp)
  554. register struct monst *mtmp;
  555. {
  556.     register char *bp = Monnam(mtmp);
  557.  
  558.     if(!strncmp(bp, "The ", 4) && !type_is_pname(mtmp->data)) {
  559.         if(index(vowels,*(bp+4))) {
  560.             *((++bp)+1) = 'n';
  561.         } else
  562.             bp += 2;
  563.         *bp = 'A';
  564.     }
  565.     return(bp);
  566. }
  567.  
  568. char *
  569. defmonnam(mtmp)
  570. register struct monst *mtmp;
  571. {
  572.     register char *bp = Xmonnam(mtmp);
  573.  
  574.     if (!strncmp(bp,"A ",2) || !strncmp(bp,"An ",3)) *bp = 'a';
  575.     return(bp);
  576. }
  577.  
  578. const char *
  579. rndmonnam() {  /* Random name of monster type, if hallucinating */
  580.     int name;
  581.  
  582.     do {
  583.         name = rn2(PM_ARCHEOLOGIST);
  584.         /* archeologist: first player class */
  585.     } while(type_is_pname(&mons[name]) || (mons[name].geno & G_NOGEN));
  586.     return(mons[name].mname);
  587. }
  588.  
  589. const char *pronoun_pairs[][2] = {
  590.     {"him", "her"}, {"Him", "Her"}, {"his", "her"}, {"His", "Her"},
  591.     {"he", "she"}, {"He", "She"},
  592.     {0, 0}
  593. };
  594.  
  595. char *
  596. self_pronoun(str, pronoun)
  597. const char *str;
  598. const char *pronoun;
  599. {
  600.     static char NEARDATA buf[BUFSZ];
  601.     register int i;
  602.  
  603.     for(i=0; pronoun_pairs[i][0]; i++) {
  604.         if(!strncmp(pronoun, pronoun_pairs[i][0], 3)) {
  605.             Sprintf(buf, str, pronoun_pairs[i][flags.female]);
  606.             return buf;
  607.         }
  608.     }
  609.     impossible("never heard of pronoun %s?", pronoun);
  610.     Sprintf(buf, str, pronoun_pairs[i][0]);
  611.     return buf;
  612. }
  613.  
  614. #ifdef REINCARNATION
  615. const char *
  616. roguename() /* Name of a Rogue player */
  617. {
  618.     char *i, *opts;
  619.  
  620.     if(opts = getenv("ROGUEOPTS")) {
  621.         for(i=opts; *i; i++)
  622.             if (!strncmp("name=",i,5)) {
  623.                 char *j;
  624.                 if (j=index(i+5,','))
  625.                     *j = (char)0;
  626.                 return i+5;
  627.             }
  628.     }
  629.     return rn2(3) ? (rn2(2) ? "Michael Toy" : "Kenneth Arnold")
  630.         : "Glenn Wichman";
  631. }
  632. #endif
  633.  
  634. #endif /* OVLB */
  635.